﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace MLY.UserControls
{



    public partial class columnSorter : System.Web.UI.UserControl
    {
        #region PRIVATE_MEMBERS

        public EventHandler eh;
        public event EventHandler clickedX;

        //does it require special handeling - FK
        bool isSpecial = false;
        public DbType currType;
        public string colName;
        KbmCalendar calExtender = null;
        KbmCalendar cal2Extender = null;
        #endregion

        #region private methods
        public columnSorter()
        { 
        
        
        }
        public columnSorter(string colname, DbType myType)
        {
          
            
            if (colname.Contains('.'))
            {
                isSpecial = true;
                colName = colname.Substring(colname.IndexOf('.') + 1);
            }
            else
                colName = colname;
            currType = myType;
            switch (currType)
            {
                case DbType.String: setToString();
                    break;
                case DbType.Int32: setToInt();
                    break;
                case DbType.DateTime: setToDate();
                    break;

            }
            this.DataBind();

        }
        void setToString()
        {
            operatorDdl.Items.AddRange(new ListItem[] { new ListItem("Like","like") , 
                    new ListItem("Equals","=")});
        }
        void setToDate()
        {
            date2Chooser.Visible = true;
            Random R = new Random();
            R.Next(100);
            calExtender = new KbmCalendar();
            cal2Extender = new KbmCalendar();
            calExtender.ID = "ext1" + R.Next(10000);
            cal2Extender.ID = "ext2" + R.Next(10000);
            calExtender.TargetControlID = "dateChooser";
            cal2Extender.TargetControlID = "date2Chooser";
            this.Controls.Add(calExtender);
            this.Controls.Add(cal2Extender);
            operatorDdl.Items.AddRange(new ListItem[] { new ListItem("Before","<") , 
                    new ListItem("After",">"),new ListItem("Equal","="),new ListItem("BETWEEN","BETWEEN")});

        }
        void setToInt()
        {
            operatorDdl.Items.AddRange(new ListItem[] { new ListItem(">",">") , 
                    new ListItem("=","="),new ListItem("<","<")});
        }
        public string parseDate()
        {
            DateTime valDate,valDate2;
            
            if (!DateTime.TryParse(dateChooser.Text, out valDate))
            {
                valDate = DateTime.Now.AddDays(-20);
            }
              if (!DateTime.TryParse(date2Chooser.Text, out valDate2))
            {
                valDate2 = DateTime.Now;
            }

            if(operatorDdl.SelectedValue == "BETWEEN")
                return "'" + valDate.ToString(@"MM/dd/yyyy") + "' AND '" + valDate2.ToString(@"MM/dd/yyyy") + "'";
            else
                return "'" + valDate.ToString(@"MM/dd/yyyy") + "' ";

        }
        public string parseString()
        {
            if (dateChooser.Text != "")
            {
                if (operatorDdl.SelectedValue == "like")
                    return "'%" + dateChooser.Text + "%'";
                else
                    return "'" + dateChooser.Text + "'";

            }
            else
            {
           //     errMsg.Text = Dictionary.Translate("addWo.StringParseError");
                return "";
            }
        }
        public string parseInt()
        {
            Int32 valNum;
            if (!Int32.TryParse(dateChooser.Text, out valNum))
            {
       //         errMsg.Text = Dictionary.Translate("addWo.intParseError");
                return "";
            }
            return valNum.ToString();
        }

        public string fkName(bool isContractor)
        {
            errMsg.Text = "";
            System.Collections.Generic.List<int> mylist;
            Dal.lqKbmDataContext dc = new MLY.Dal.lqKbmDataContext();
            if (operatorDdl.SelectedItem.Value == "like")
                mylist = (from p in dc.TableOrganizations
                          where p.OrganizationName.Contains(dateChooser.Text)
                          select p.OrganizationId).ToList();
            else
                mylist = (from p in dc.TableOrganizations
                          where p.OrganizationName == dateChooser.Text
                          select p.OrganizationId).ToList();
            if (mylist.Count == 0)
                return (" 0 = 1 ");
            string arr = "(";
            foreach (int i in mylist)
                arr += i.ToString() + ",";
            arr = arr.Remove(arr.Length - 1);
            arr += ")";
            if (isContractor)
                return " CustomerId in " + arr;
            else
                return " ContractorId in " + arr;
        }



        /// <summary>
        /// if called from contractor - returns where clientIds in array , else returns where ContractorIds in array - in fkReturn
        /// </summary>
        /// <param name="isContractor"></param>
        /// <returns></returns>
        public string getWhere(bool isContractor)
        {
            Parameter retVal = new Parameter();
            string valParse = "";
            switch (currType)
            {

                case DbType.DateTime: valParse = parseDate();
                    break;
                case DbType.Int32: valParse = parseInt();
                    break;
                case DbType.String: valParse = parseString();
                    break;
            }
            //RETURNS CUSTOMER/CONTRACTOR IDS
            if (isSpecial)
                return fkName(isContractor);

            if (valParse != "")
            {
                errMsg.Text = "";
                return colName + " " + operatorDdl.SelectedItem.Value + " " + valParse;
            }
            else
                return valParse;
        }

        #endregion

        #region event_Handlers
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            clickedX(this, EventArgs.Empty);
        }
        #endregion




        
    }
}